package phase1.part3;

import java.util.HashSet;
import java.util.Set;

/**
 * 
 * 
 * @author John Girata
 * @version Nov 1, 2012
 */
public class Matcher {
	private String pattern;
	private Set<Character> accepting;

	public Matcher(String pattern) {
		this.pattern = pattern;
		accepting = new HashSet<>();

		char[] chars = pattern.toCharArray();

		for (int i = 0; i < chars.length; i++) {
			if (chars[i] == '-' && i > 0 && i < chars.length - 1) {
				for (char c = chars[i - 1]; c < chars[i + 1]; c++) {
					accepting.add(c);
				}
			} else {
				accepting.add(chars[i]);
			}
		}
	}

	public boolean accepts(char c) {
		return accepting.contains(c);
	}

	

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((accepting == null) ? 0 : accepting.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof Matcher))
			return false;
		Matcher other = (Matcher) obj;
		if (accepting == null) {
			if (other.accepting != null)
				return false;
		} else if (!accepting.equals(other.accepting))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Matcher [pattern=" + pattern + ", accepting=" + accepting + "]";
	}
}
